<?php
/**
 * Created by PhpStorm.
 * User: 跳梁
 * Date: 2016/2/23
 * Time: 11:09
 */
class UserGroupAction extends Action{
    //分组列表
    public function userGroupList($group_name=null,$start=0,$limit=200){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $usergroup = M("user_weixin_group");
        isset($group_name)&&$group_name?$condition['group_name']=array('like','%'.$group_name.'%'):"";
        $group = $usergroup->where($condition)->order("group_id")->limit($start,$limit)->select();
        $count = $usergroup->where($condition)->count();
        Output::instance()->objArray($group,$count);
    }
    //获得分组信息
    public function getGroupInfo($id=''){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $usergroup = M("user_weixin_group");
        $condition['id']=array('eq',$id);
        $group = $usergroup->where($condition)->find();
        Output::instance()->object($group);
    }
    //刷新微信用户分组信息 同步到表
    public function refreshGroup(){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $userGroup = M("user_weixin_group");
        $groupInfo = WeixinApi::getUserTag();
        $userGroup->where('1<>2')->delete();
        $groupInfo = json_decode($groupInfo,true);
        $groups = $groupInfo['tags'];
        foreach($groups as $key=>$value){
            $data[$key]['group_id']=$value['id'];
            $data[$key]['group_name']=$value['name'];
            $data[$key]['count']=$value['count'];
        }
        $result = $userGroup->addAll($data);
        if($result){
            Output::instance()->resultCode(code_success);
        }else{
            Output::instance()->resultCode(code_sql_error);
        }
    }
    //创建用户分组
    public function createUserGroup($groupName){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $userGroup = M("user_weixin_group");
        $result = WeixinApi::createUserTag($groupName);
        $result = json_decode($result,true);
        if($result['errcode']){
            Output::instance()->resultCode($result['errcode'],$result['errmsg']);
        }else{
            $data['group_id']=$result['tag']['id'];
            $data['group_name']=$result['tag']['name'];
            $data['count']=0;
            $userResult = $userGroup->add($data);
            if($userResult){
                Output::instance()->resultCode(code_success);
            }else{
                Output::instance()->resultCode(code_sql_error);
            }
        }
    }
    //修改分组名称
    public function setUserGroupName($groupId,$groupName){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $userGroup = M("user_weixin_group");
        $groupInfo = $userGroup->where("id=".$groupId)->find();
        $result = WeixinApi::setUserTagName($groupInfo['group_id'],$groupName);
        $result = json_decode($result,true);
        if($result['errcode']){
            Output::instance()->resultCode($result['errcode'],$result['errmsg']);
        }else{
            $condition['id']=array('eq',$groupId);
            $data['group_name']=$groupName;
            $userResult = $userGroup->where($condition)->save($data);
            if($userResult!==false){
                Output::instance()->resultCode(code_success);
            }else{
                Output::instance()->resultCode(code_sql_error);
            }
        }
    }
    //删除用户分组
    public function deletedUserGroup($groupId){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $userGroup = M("user_weixin_group");
        $groupInfo = $userGroup->where("id=".$groupId)->find();
        $result = WeixinApi::deletedUserTag($groupInfo['group_id']);
        $result = json_decode($result,true);
        if($result['errcode']){
            Output::instance()->resultCode($result['errcode'],$result['errmsg']);
        }else{
            $condition['id']=array('eq',$groupId);
            $userResult = $userGroup->where($condition)->delete();
            if($userResult!==false){
                Output::instance()->resultCode(code_success);
            }else{
                Output::instance()->resultCode(code_sql_error);
            }
        }
    }
}